Loops in C

In C language, loops are used to execute set of statements continously until the condition is satisfied.

loops in c

In the above diagram, initially the condition is check, if the condition is true, then it will continue executing the block of statements in the loop. When the condition gets failed it stops executing the loop and exits the loop.

There are 3 types of loops:

  1. for loop

  2. while loop

  3. do-while loop

for loop

The for loop is used to execute the set of statements until a particular condition is satisfied. The for loop will be containing the initialization, condition check and increment/decrement operation.

Syntax

for(initialization;condition;increment/decrement)
{
	// Block of statement.
}

The initialization, condition check and increment/decrement are seperated by semi-colon(;).

*) The initialization is done only once during the first execution to the loop.

*) The condition is checked before each execution of the statement.

*) The statement is executed only if the condition is true. If the condition fails, then it will exit the loop.

*) The increment/decrement is used to increment or decrement the variable for each iteration.

Example

#include<stdio.h>

void main( )
{
    int x;
    for(x = 1; x <= 5; x++)
    {
        printf("%d\t", x);
    }
}

Output

1 2 3 4 5

Example program to find sum of n numbers

#include<stdio.h>

void main( )
{
    int n;
    int result = 0;
    printf("Enter a number to find sum of n numbers");
    scanf(#%d",&n);
    for(i=1;i<=n;i++)
    {
        result = result + i;
    }
    printf("Result is %d",result);
}

Output

5
Result is 15

while loop

The while loop is used to check the condition before entering the loop. The while loop contains only the condition. It is also called as entry control loop.

Syntax

while(condition)
{
	// block of statements.
}

Lets see an example program to print sum of n numbers using while loop.

#include<stdio.h>

void main( )
{
    int n;
    int result = 0;
    printf("Enter a number to find sum of n numbers");
    scanf("%d",&n);
    int i = 1;
    while(i<=n)
    {
        result = result + i;
        i++;
    }
    printf("Result is %d",result);
}

Output

5
Result is 15

In the above program, we have initialized the value of i before the while loop and then check the condition in while loop, if the condition is satisfied, then the value of i will be added to the result.

At the end of while loop we will increment the value of i, so that for next iteration the i will have the next integer.

do-while loop

The do-while loop is used in some cases like when we want to execute the body of the loop before the condition check. The do statement evaluates the body of the loop first and then the condition is checked using while statement. So the body of the loop is executed at least once, even if the condition is not true initially.

Syntax

do { 
    // Block of statements 
} while(condition);

Example for do-while loop

#include <stdio.h>
int main()
{
	int i=0;
	do
	{
		printf("i is: %d\n", i);
		i++;
	}while(i<=5);
	return 0;
}

Output

0 1 2 3 4 5

Nested for loop

Sometimes we need to add a for loop inside an other for loop. In such cases we need to nest the for loop.

Syntax

for(initialization; condition; increment/decrement)
{
    for(initialization; condition; increment/decrement)
    {
        block of statement;
    }
}

Example to print a star pattern

#include<stdio.h>

void main( )
{
    int i, j;
    /* first for loop */
    for(i = 1; i < 5; i++)
    {
        printf("\n");
        /* second for loop inside the first for loop*/
        for(j = 1; j >= i; j++)
        {
            printf("*");
        }
    }
}

Output

*
**
***
****
*****

In the above program, the first for loop is used to mention the number of rows and the second for loop is used to print the * based on the value of i variable.

If the value of i is 1 then, the second for loop will print one * in the first row. If the value of i is 2 then, the second for loop will print two * in the second row.


Most Read